﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace YinLongTool.Pc
{
    public class AutoDialer
    {
        public void Connect(string connectionName, string user, string pass)
        {
            string cmdArgs = $"rasdial \"{connectionName}\" \"{user}\" \"{pass}\"";
            InvokeCmd(cmdArgs);
        }

        public void Disconnect(string connectionName)
        {
            string cmdArgs = $"rasdial \"{connectionName}\" /disconnect";
            InvokeCmd(cmdArgs);
        }

        private static string InvokeCmd(string cmdArgs)
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            process.StandardInput.WriteLine(cmdArgs);
            process.StandardInput.WriteLine("exit");
            return process.StandardOutput.ReadToEnd();
        }

        public string TestConnect(string connectionName, string user, string pass)
        {
            string cmdArgs = $"rasdial \"{connectionName}\" \"{user}\" \"{pass}\"";
            return InvokeCmd(cmdArgs);
        }

        public static string[] GetAdslName()
        {
            string name = "RemoteAccess\\Profile";
            RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(name);
            if (registryKey != null)
            {
                return registryKey.GetSubKeyNames();
            }
            return new string[0];
        }
    }
}
